<?php


namespace app\model;


use think\Model;

class Admin extends Model
{
    //主键
    protected $pk = "aid";
    //表名
    protected $name = "admin";
    // 设置当前模型对应的完整数据表名称
    protected $table = 'jw_admin';

    public function profileRole()
    {
        return $this->hasOne(Role::class,'role','rid');
    }

    protected $schema = [
        'account' => 'string',//账号'
        'password' => 'string',//密码'
        'mobile' => 'string',//手机号'
        'status' => 'int',//是否拉黑 1 未拉黑  2拉黑'
        'addtime' => 'datetime',//注册时间'
        'lasttime' => 'datetime',//更新时间'
        'register_ip' => 'string',//注册IP'
        'last_ip' => 'string',//最后一次IP'
        'role' => 'int',//权限ID'
        'login' => 'int',//登录次数'
    ];

    //入库添加数据
    public static function 	onBeforeInsert($user)
    {
        $user->register_ip = get_client_ip();
        $user->last_ip = get_client_ip();
        $user->password = set_password($user->password);
    }

    //自动修改密码
    public function setPasswordAttr($value)
    {
        return set_password($value);
    }

    public static function onBeforeUpdate($user)
    {
        $user->last_ip = get_client_ip();
    }




}